<?php
include '../includes/cDatabase.php';
include '../includes/variables.php';
include '../includes/cScores.php';
include '../includes/cPage.php';
include '../includes/cDates.php';

$cPage = new page("Add Scores");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

function addPlayerScoresMenu($playerCode, $playerName, $numberPlayers, $week, $weekScore, $updateFlag)
{
	$playerScoreArray = $_POST['playerScoreArray'];
	$playerCodeArray = $_POST['playerCodeArray'];
	
	if ($updateFlag == 1)
		$buttonLabel = "Update Scores";
	else
		$buttonLabel = "Add Scores";
		
	echo "\n\n<form action=";
	echo "DB_ProcessScores.php?numberPlayers=".$numberPlayers."&week=".$week;
	echo " method='POST'>";
  	echo "\n<table border='0'>";
	for ($i = 0; $i < $numberPlayers; $i++)
	{ 
		echo "\n<tr>\n<td>";
		echo "<INPUT TYPE='text' readonly='true' NAME='playerCodeArray[]' VALUE='$playerCode[$i]' size='4' maxlength='4'>";
		echo "</td>\n<td>".$playerName[$i]."</td>";
		echo "\n<td><INPUT TYPE='text' NAME='playerScoreArray[]' VALUE='$weekScore[$i]' size='3' maxlength='3'>";
		echo "</td>\n</tr>";
	}
	echo "\n</table>\n<table>";
	echo "\n<td colspan='2' align='center'>";
	echo "<input name='addScoresButton' type='submit' id='addScores' value='".$buttonLabel."'>";
	echo "</td>\n</table>\n</form>\n\n";
}

function sortPlayerArray($playerCode)
{
	// These arrays will store the sorted codes
	$goalkeeperCodeArray = array();
	$defenderCodeArray = array();
	$midfielderCodeArray = array();
	$attackerCodeArray = array();
	$sortedPlayerCodeArray = array();

	// This is the number of each player type that exists - used as indexes into the array
	$goalkeeperNumber = 0;
	$defenderNumber = 0;
	$midfielderNumber = 0;
	$attackerNumber = 0;

	$cDatabase = new database();

	$connect = $cDatabase->connectToDatabase();
	$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
	$db = $cDatabase->selectDatabase($connect, $activeDatabase);

	$selectArray[0] = 'playerPosition';
	$selectArray[1] = 'playerValue';
	
	if ($result = $cDatabase->selectFromDatabase($connect, 'playerlist', $selectArray, NULL, NULL, NULL, NULL))
	{
		// Go through every row (player)
		for ($playerNumber = 0; $playerNumber < mysqli_num_rows($result); $playerNumber++)
		{
			$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

			// Find out what position thsi player is and insert his VALUE into the respective array and increment the index
			if ($row['playerPosition'] == 'GK')
			{
				$goalkeeperValueArray[$goalkeeperNumber] = $row['playerValue'];
				$goalkeeperNumber++;
			}
			if ($row['playerPosition'] == 'DEF')
			{
				$defenderValueArray[$defenderNumber] = $row['playerValue'];
				$defenderNumber++;
			}
			if ($row['playerPosition'] == 'MID')
			{
				$midfielderValueArray[$midfielderNumber] = $row['playerValue'];
				$midfielderNumber++;
			}
			if ($row['playerPosition'] == 'ATT')
			{
				$attackerValueArray[$attackerNumber] = $row['playerValue'];
				$attackerNumber++;
			}
		}
	}

	// We need to sort this array into the correct order that is displayed in the paper
	rsort($goalkeeperValueArray);
	rsort($defenderValueArray);
	rsort($midfielderValueArray);
	rsort($attackerValueArray);

	// Reset array pointers
	$goalkeeperNumber = 0;
	$defenderNumber = 0;
	$midfielderNumber = 0;
	$attackerNumber = 0;

	$selectArray[0] = 'playerCode';
	$selectArray[1] = 'playerValue';
	$selectArray[2] = 'playerPosition';

	if ($result = $cDatabase->selectFromDatabase($connect, 'playerlist', $selectArray, NULL, NULL, NULL, NULL))
	{
		$goalkeeperCodeArray = sortThisArray($goalkeeperValueArray, $result, 'GK');
		$defenderCodeArray = sortThisArray($defenderValueArray, $result, 'DEF');
		$midfielderValueArray = sortThisArray($midfielderValueArray, $result, 'MID');
		$attackerValueArray = sortThisArray($attackerValueArray, $result, 'ATT');
	}

	// Now add these particular arrays to the sortedPlayerCodeArray
	$index = 0;

	$j = 0;
	for ($i = $index; $i < sizeof($goalkeeperCodeArray); $i++)
	{
		$sortedPlayerCodeArray[$i] = $goalkeeperCodeArray[$j];
		$j++;
	}
	$index += i;
	echo "def size ".sizeof($defenderCodeArray);
	$j = 0;
	for ($i = index; $i < sizeof($defenderCodeArray); $i++)
	{
		$sortedPlayerCodeArray[$i] = $defenderCodeArray[$j];
		$j++;
	}
	$index += i;

	$j = 0;
	for ($i = $index; $i < sizeof($midfielderValueArray); $i++)
	{
		$sortedPlayerCodeArray[$i] = $midfielderValueArray[$j];
		$j++;
	}
	$index += i;

	$j = 0;
	for ($i = index; $i < sizeof($attackerValueArray); $i++)
	{
		$sortedPlayerCodeArray[$i] = $attackerValueArray[$j];
		$j++;
	}

	return $sortedPlayerCodeArray;
}

function sortThisArray($thisValueArray, $result, $playerPosition)
{
	$thisNumber = 0;

	// Go though all players in this array
	for ($i = 0; $i < sizeof($thisValueArray); $i++)
	{
		$foundIt = 0;
		mysqli_data_seek($result, 0);	// Reset pointer, we now want to look at the start of the list of players again

		// Go through each player in the database until we find our player then set foundIt to 1
		for($playerNumber = 0; $playerNumber < mysqli_num_rows($result); $playerNumber++)
		{
			if ($foundIt != 1)
			{
				// Get the row for this particular player
				$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

				if (($row['playerValue'] == $thisValueArray[$i]) && ($row['playerPosition'] == $playerPosition))
				{
					// Now check that we don't already have this player in our array
					$playerAlreadyAdded = 0;
					for($j = 0; $j <= sizeof($thisCodeArray); $j++)
					{
						if ($row['playerCode'] == $thisCodeArray[$j])
						{
							// It's already in the array
							$playerAlreadyAdded = 1;
						}
					}

					if ($playerAlreadyAdded == 0)
					{
						// We haven't added this player yet so do it now
						$thisCodeArray[$thisNumber] = $row['playerCode'];
						$thisNumber++;
						$foundIt = 1;	// We've found it, set the flag so we don't go through the loop again
					}
				}
			}
		}
	}
	//for ($i = 0; $i < sizeof($thisCodeArray); $i++)
	//	echo "<BR>code:".$thisCodeArray[$i]." ".$thisValueArray[$i];
	
	return $thisCodeArray;
}

$cDatabase = new database();

$connect = $cDatabase->connectToDatabase();
$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
$db = $cDatabase->selectDatabase($connect, $activeDatabase);
	
if(@$_GET['form'] == "yes")
{
	$playerCode = array();
	$playerName = array();
	$weekScore = array();
	
	$thisWeek = $_POST['selectWeek'];
	
	echo"<BR>Add scores for week ".$thisWeek."/".$numberWeeks;

	// Do some checks on the weeks
	$cScores = new scores();
	$cScores->CheckWeeks($thisWeek, $cDatabase, $connect);
	
	$selectArray[0] = '*';
	$result = $cDatabase->selectFromDatabase($connect, 'playerlist', $selectArray, NULL, NULL, NULL, NULL);
	$NumberPlayers = mysqli_num_rows($result);
	
	for ($i = 0; $i < $NumberPlayers; $i++)
	{
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
		$playerCode[$i] = $row['playerCode'];
		$playerName[$i] = $row['playerName'];
	}
	
	$result = addPlayerScoresMenu($playerCode, $playerName, $NumberPlayers, $thisWeek, $weekScore, $updateFlag);
}
else
{
	$selectArray[0] = '*';
	$result = $cDatabase->selectFromDatabase($connect, 'playerlist', $selectArray, NULL, NULL, NULL, NULL);
	
	// Get the number of weeks from the DB
	$finfo = mysqli_fetch_fields($result);
	$numberWeeks = 0;
	for ($i = 0; $i < mysqli_num_fields($result); $i++)
	{
		if ( ( substr(trim($finfo[$i]->name), 0 , 4) == 'week'))
			$numberWeeks++;
	}
	?>
	<form action="<?php echo $_SERVER['PHP_SELF'] ?>?form=yes&numberWeeks= <?php echo $numberWeeks ?>" method="POST">
	  	<table border='0'>
			<tr>
			  <td colspan="2" align="center">
	    		<select name="selectWeek">
				<?php
				for ($j = 1; $j <= $numberWeeks; $j++)
				{
	    		  	echo"<option value = '" .$j. "'>" .$j. "</option>";
				}
				?>
		        </select>
	    		<input name="selectWeekButton" type="submit" id="selectWeek" value="Select Week">
		 	</tr>
	  	</table>
	</form>
	<?php
}
echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?>